<?php

// Created by Khanh Nam
class MyMenu extends CWidget{
    //each item will have label, url, active 
    public $items;
    public function init(){
        
        if(isset($this->items) && count($this->items>0)){
            //check for active route
            foreach($this->items as $i=>$item){
                
                if(isset($item['items']) && count($item['items'])>0){
                    foreach($item['items'] as $t=>$subitem){
                        if(isset($subitem['activeRoute']) && count($subitem['activeRoute'])>0){
                            foreach($subitem['activeRoute'] as $route){
                                
                                if(Yii::app()->controller->id==$route){
                                    $this->items[$i]['items'][$t]['active']=true;
                                    
                                }
                            }
                        }
                    }
                }
            }
            
            //if subitem is active then parent menu is active too
            foreach($this->items as $i=>$item){
                
                if(isset($item['items']) && count($item['items'])>0){
                    foreach($item['items'] as $t=>$subitem){
                        if(isset($subitem['active']) && $subitem['active']==true){
                            $this->items[$i]['active']=true;                            
                        }
                    }
                }
            }
        }
        
     
    }
    public function run(){
        $this->render('application.components.view.MyMenu',array(
            'items'=>$this->items,
        ));
    }
}
?>
